package controllers;

import java.util.ArrayList;
import java
.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import model.entities.DirListUpdated;
import myDaos.DirListDao;

@ManagedBean
@ViewScoped
public class DirsManagedBean {
	
private List<DirListUpdated> dlist;
private String fileName;
private String country;
private String region;
private Date from;
private Date to;
private boolean isMethodCalled = false;
private List<SelectItem> countriesList;
private List<String> countries;
 

@EJB
private DirListDao dld;
 

@ManagedProperty("#{servicesManagedBean}")
private ServicesManagedBean servicesManagedBean;





public List<String> getCountries() {
	return countries;
}


public void setCountries(List<String> countries) {
	this.countries = countries;
}


public List<SelectItem> getCountriesList() {
	return countriesList;
}


public void setCountriesList(List<SelectItem> countriesList) {
	this.countriesList = countriesList;
}


public ServicesManagedBean getServicesManagedBean() {
	return servicesManagedBean;
}


public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
	this.servicesManagedBean = servicesManagedBean;
}


public boolean isMethodCalled() {
	return isMethodCalled;
}


public void setMethodCalled(boolean isMethodCalled) {
	this.isMethodCalled = isMethodCalled;
}


public Date getFrom() {
	return from;
}


public void setFrom(Date from) {
	this.from = from;
}


public Date getTo() {
	return to;
}


public void setTo(Date to) {
	this.to = to;
}


public String getCountry() {
	return country;
}


public void setCountry(String country) {
	this.country = country;
}


public String getFileName() {
	return fileName;
}


public void setFileName(String fileName) {
	this.fileName = fileName;
}


public List<DirListUpdated> getDlist() {
	return dlist;
}


public void setDlist(List<DirListUpdated> dlist) {
	this.dlist = dlist;
}


public String getRegion() {
	return region;
}


public void setRegion(String region) {
	this.region = region;
}


public void updateList()
{
	dlist=dld.getDirsByFilter(fileName,country,from,to,region);
	isMethodCalled = true;
}


public void updateCountries()
{
	//Proposed by Samy
	countriesList = new ArrayList<SelectItem>();
	countries = new ArrayList<String>();
	for(int i = 0; i < servicesManagedBean.getCountrieslist().size(); i++) 
    {  
		if(region!=null && !region.trim().equals("") && 
				  servicesManagedBean.getCountrieslist().get(i).getMapRegion().equals(region))
		{
			if(!countries.contains(servicesManagedBean.getCountrieslist().get(i).getMapCountry())){
				countries.add(servicesManagedBean.getCountrieslist().get(i).getMapCountry());
				countriesList.add(new SelectItem(servicesManagedBean.getCountrieslist().get(i).getMapCountry(),
						servicesManagedBean.getCountrieslist().get(i).getMapCountry()));
			}
		}
    }
}


@PostConstruct
public void init(){
	to = new Date();
	GregorianCalendar gc = new GregorianCalendar();
	gc.setTime(to);
	gc.add(Calendar.DAY_OF_YEAR, -1);
	from = gc.getTime();
}


}
